﻿<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DGM.Web.SocialAnalyzerHost._Default" %>

<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
    <script src="/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script src="/Scripts/swfobject.js" type="text/javascript"></script>
    <script src="/Scripts/json2.js" type="text/javascript"></script>
    <script src="/Scripts/jXSockets.WebSockets.Detector-1.0.4.beta.js" type="text/javascript"></script>        
    <script src="/Scripts/JXSockets.1.0.6.beta.js" type="text/javascript"></script>
    <script src="/Scripts/jquery.sparkline.js" type="text/javascript"></script>
    <script type="text/javascript">

        var realTimeDic = new Object();
        var sec10Dic = new Object();
        //var realTimeValues = [0];
        //var sec10Values = [0];
        //var sec10BarValues = [0];
        

        var ws = null;
        $(document).ready(function () {
            ws = new XSockets.WebSocket("ws://127.0.0.1:4503/SocialAnalyzerWebSocket", "SocialAnalyzerWebSocket");

            attachEvents();
        });
        
        // Deal with our WebSockets Events
        function attachEvents() {
            //We listen to the event that we also trigger onclick.
            ws.bind("OnTweetMessage", onTweetReceived);
            ws.bind("On10SecMessage", On10SecMessage);
            ws.bind("On60SecMessage", On10SecMessage);
        }
        
        function On10SecMessage(classifiedTweetString) {
            var classifiedTweet = eval("(" + classifiedTweetString + ')');

            var categoryString = classifiedTweet.CategoryGroup;

            var sec10Values = CategoryCheck10Sec(categoryString);

            if (sec10Values.length >= 250) {
                sec10Values.splice(0, 1);
            }
            sec10Values.push(classifiedTweet.CalculatedMasterValue);
            $('#dynamicsparklineRealtime10Secs').sparkline(sec10Values);
        }

        function onTweetReceived(classifiedTweetString) {
            var classifiedTweet = eval("(" + classifiedTweetString + ')');

            var categoryString = classifiedTweet.CategoryGroup;

            var realTimeValues = CategoryCheck(categoryString);

            if (realTimeValues.length >= 250) {
                var indexToRemove = 0;
                var numberToRemove = 1;

                realTimeValues.splice(indexToRemove, numberToRemove);
            }

            realTimeValues.push(classifiedTweet.CalculatedMasterValue);
            $('#' + categoryString + 'dynamicsparklineRealtime').sparkline(realTimeValues);

            /*var html = '<p><b>' + classifiedTweet.CategoryGroup + '</b><br />';
            html = html + 'Positive: ' + classifiedTweet.CalculatedPositive + '<br />';
            html = html + 'Negative: ' + classifiedTweet.CalculatedNegative + '<br /></p>';
 
            $("#devDiv").append(html);*/
        }

        function CategoryCheck10Sec(categoryString) {
            CategoryCheck(categoryString);
            
            if (sec10Dic[categoryString] == undefined) {
                sec10Dic[categoryString] = [0];
            }

            return sec10Dic[categoryString];
        }

        function CategoryCheck(categoryString) {
            if(realTimeDic[categoryString] == undefined) {
                realTimeDic[categoryString] = [0];
                $("#holder").append(Template(categoryString));
            }

            return realTimeDic[categoryString];
        }

        function Template(categoryString) {
            var templateString = '<div><p><b>' + categoryString + '</b></p><span id="' + categoryString + 'dynamicsparklineRealtime">Loading..</span></div>';
            templateString = templateString + '<div><span id="' + categoryString + 'dynamicsparklineRealtime10Secs">Loading..</span></div>';
            //templateString = templateString + '<div><span id="' + categoryString + 'dynamicBarRealtime10Secs">Loading..</span></div>';
            return templateString;
        }

    </script>
    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1>McGregorBFA Social Analyser</h1>
            </hgroup>
            <h2>See heartbeat of the companies</h2>
        </div>
    </section>
</asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <div id="holder">
        
    </div>
</asp:Content>
